// pages/quality-info/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    showSure: false,
    iconActive: false,
    hasMore: true,
    active: "0",
    value: "",
    type: "",
    qualityNoIsError: "",
    qualityNo: "",
    qualityId: "",
    param: { page: 1, pageSize: 10 },
    list: [],
  },

  handleTabChange(event) {
    const { value } = event.currentTarget.dataset;
    const { param } = this.data;

    param.page = 1;

    this.setData({
      active: value,
      param,
      list: [],
    });

    this.getData();
  },

  showSeviceChange(event) {
    const { value } = event.currentTarget.dataset;

    this.setData({
      active: value,
      show: true,
      value: '',
      list: []
    });
  },

  handleShowSure(event) {
    this.setData({
      showSure: true,
      qualityId: event.currentTarget.dataset.id,
    });
  },

  handelToUpload(event) {
    wx.navigateTo({
      url: "/quality/pages/quality-upload/index?id=" + event.currentTarget.dataset.id,
    });
  },

  handleQualityNoChange(event) {
    const value = event.detail;

    this.setData({
      qualityNoIsError: value ? "" : "请输入质保编号",
    });
  },

  handelCancelSureDialog() {
    this.setData({
      showSure: false,
    });
  },

  handleConfirmSureDialog() {
    const { qualityNo, qualityId } = this.data;

    if (qualityNo) {
      const params = {
        id: qualityId,
        quality_code: qualityNo,
      };
      wx.ajax.post("confirmQualityInfo.action", params, true).then((res) => {
        if (res.success) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });

          const { param } = this.data;
          param.page = 1;

          this.setData({
            showSure: false,
            param,
            list: [],
          });

          this.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });
        }
      });
    } else {
      this.setData({
        qualityNoIsError: "请输入质保编号",
      });
    }
  },

  handleShowFilter() {
    const {active} = this.data
    if (active == 2) {
      return
    }

    this.setData({
      show: !this.data.show,
    });
  },

  handelGetMore() {
    const { hasMore, param } = this.data;

    if (!hasMore) return;

    param.page++;

    this.setData({
      param,
    });

    this.getData();
  },
  // 请求管理图小区
  requestSearchShiya() {
    wx.ajax.post('queryShiyaList.action', {serviceNo: this.data.value}, true).then(res => {
      if (res.success) {
        this.setData({
          list: res.result
        })
      }
    })
  },

  getData() {
    const { value, active, param, type } = this.data;

    const params = {
      status: active === "1" ? "1" : "",
      keyword: value,
      ...param,
    };

    wx.ajax
      .post(
        type === "7"
          ? "queryPageUserQualityInfo.action"
          : "queryPageMerchantQualityInfo.action",
        params,
        true
      )
      .then((res) => {
        // console.log(res);
        if (res.success) {
          const result = res.result;
          const oriList = this.data.list;

          this.setData({
            list: oriList.concat(result.pageList || []),
            hasMore: result.totalPages > result.page,
          });
        }
      });
  },

  handleChange(event) {
    this.setData({
      value: event.detail,
    });
  },

  handleSearch() {
    const { value, param, active } = this.data;
    param.page = 1;

    this.setData({
      show: false,
      iconActive: !!value,
      param,
      list: [],
    });

    if (active == 2) {
      this.requestSearchShiya()
    } else {
      
      this.getData();
    }
  },

  handleToDetail(event) {
    wx.navigateTo({
      url:
        "/quality/pages/quality-info-detail/index?data=" +
        JSON.stringify(event.currentTarget.dataset.item),
    });
  },

  handleReportDetail(event) {
    const id = event.currentTarget.dataset.pressureApplyId;
    const type = event.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/vr/pages/vr-report/vr-report?infoId=" + id+"&findVrType="+type,
    });
  },
  handleDetail(event) {
    wx.navigateTo({
        url:
          "/pages/shiya-img-video/shiya-img-video?recordId=" + event.currentTarget.dataset.recordId+"&serviceNo="+event.currentTarget.dataset.serviceNo+"&infoId="+event.currentTarget.dataset.pressureApplyId,
      });
  },
  handleMapDetail(event) {
    wx.navigateTo({
      url:
        "/pages/shiya-map/index?recordId=" + event.currentTarget.dataset.recordId+"&serviceNo="+event.currentTarget.dataset.serviceNo,
    });
  },
  handleVrInfo(event) {
    const id = event.currentTarget.dataset.pressureApplyId;
    const type = event.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/vr/pages/vr-info/vr-info?infoId=" + id+"&findVrType="+type,
    });
  },
  handleVideoDetail(event) {
    const id = event.currentTarget.dataset.pressureApplyId;
    const type = event.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/vr/pages/vr-video/vr-video?infoId=" + id,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    this.setData({
      type: options.type,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { param, active} = this.data;
    if (active == 2) {
      return
    }

    param.page = 1;

    this.setData({
      param,
      list: [],
    });

    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
